<?php require VIEW_ROOT.'/common/header.php'?>

<style>
.list_item { background:#ffffc4; border:#698cc3 1px solid; padding:5px 0 5px 5px; margin:1px; }
.list_item .time { background:#a00; color:#fff; margin:0 10px 0 0; font-size:12px; }
.from .content { font-weight:bold; color:#00f; }
</style>

<script language="javascript">
var _from = <?php echo $from?>;
var _to = <?php echo $to?>;
var last_lid = 0;

$(function() {
	get_list();
});

function get_list() {
	$.getJSON('http://t.a/fms/?m=chat&a=list_data&last_lid=' + last_lid + '&jsoncallback=?', function(data) {
		var html = '';
		var from_style = '';
		for(var i = data.length - 1; i >= 0; i--) {
			from_style = data[i].from == _from ? 'from' : '';
			html += '<div class="list_item ' + from_style + '">';
			html += '<span class="time">' + data[i].created_time + '</span>';
			html += '<span class="content">' + data[i].content + '</span>';
			html += '</div>';
			last_lid = data[i].lid;
		}
		$('#list').html(html);
		get_list();
	});
}

// 发送聊天内容
function send() {
	// 获取输入框内容
	var _content = $.trim($('#content').val());
	if(_content == '') {
		return false;
	}
	
	// 清空输入框
	$('#content').val('');
	
	// 提交数据
	$.post('./?m=chat&a=save', {from : _from, to : _to, content: _content}, function(data) {});
}

// 监控回车
document.onkeydown = function(e) {
	var e = window.event ? window.event : e;
	if(e.keyCode == 13) {
		send();
		return false;
	}
}
</script>

<div class="list" id="list">

</div>

<table>
	<tr>
		<td class="frm">
			<textarea name="content" id="content"></textarea>
		</td>
	</tr>
	<tr>
		<td class="frm">
			回车键发送消息
		</td>
	</tr>
</table>
</form>

<?php require VIEW_ROOT.'/common/footer.php'?>